package com.rljk.controller.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rljk.common.consts.AssetConst;
import com.rljk.common.consts.HuoBiConst;
import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;

import com.rljk.utils.DecimalUtils;
import com.rljk.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   YASUO
 * Date:     2018/8/1 9:47
 * Description:
 */
@RestController
@RequestMapping("/wallet")
@Slf4j
public class HuoBiInController extends BaseController{



    /**
     * 获取各种币种的最新价和涨幅
     * author:YASUO
     * date:2018/8/1
     */
    @RequestMapping(value="/getvariouscoin")
    public Object getVariousCoin() {
        log.info("获取各种币种的最新价和涨幅");
        List<PageData> list = new ArrayList<>();
        try {
            String url = HuoBiConst.detail;
            String s = HttpClientUtil.httpGetRequest(url);
            JSONObject json = JSONObject.parseObject(s);
            String data = json.getString("data");
            JSONObject array = JSONObject.parseObject(data);
            JSONArray symbols = array.getJSONArray("symbols");
            int size = symbols.size();
            for(int i=0;i<size;i++){
                JSONObject o = symbols.getJSONObject(i);
                PageData pd = new PageData();
                log.info("o:"+o);
                BigDecimal rise_percent = o.getBigDecimal("rise_percent");  //利率
                Object name = o.get("name");   //币种
                Object open = o.get("open");   //最新价
                pd.put("rate",DecimalUtils.toPercent(rise_percent.doubleValue()));
                pd.put("price",DecimalUtils.subZeroAndDot(open.toString()));
                pd.put("base_currency",name);
                list.add(pd);
            }
            return Result.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }




}
